/*
 * Controller.m
 *
 *	This program is the NeXTStep wrapper for import_export_mailrc.l
 *      It also provides a service for saving (portions of) mail in files.
 * 
 * Copyright (C) 1991 Roberto Di Cosmo
 *
 * Everyone is granted permission to copy, modify and redistribute
 * this file, as far as this notice is preserved on all copies.
 *
 * Written by:
 *      Roberto Di Cosmo <dicosmo@dmi.ens.fr>
 *      Dipartimento di Informatica     LIENS
 *      Universita' di Pisa             Ecole Normale Superieure
 *      Corso Italia, 40                45, Rue d'Ulm
 *      Pisa ITALY                      Paris FRANCE
 *
 * $Header: /users/formel/dicosmo/Projects/SAVEFROMMAIL/RCS/Controller.m,v 1.3 92/05/06 21:17:13 dicosmo Exp Locker: dicosmo $
 *
 * $Log:	Controller.m,v $
 * Revision 1.3  92/05/06  21:17:13  dicosmo
 * Removed SavePanel free,
 * now stays up!
 * 
 * Revision 1.2  92/05/06  16:52:39  dicosmo
 * Rev 1.2
 * 
 * Revision 1.1  92/05/06  14:18:17  dicosmo
 * Initial revision
 * 
 *
 */

/* Generated by Interface Builder */

#import <stdio.h>
#import "Controller.h"
#import <appkit/Application.h>
#import <appkit/Panel.h>
#import <appkit/Listener.h>
#import <appkit/ScrollView.h>
#import <appkit/SavePanel.h>
#import <appkit/Text.h>
#import <appkit/Pasteboard.h>
#import <appkit/TextField.h>
#import <stdlib.h>
#import <strings.h>
#import <mach_init.h>
/* #import <vm/vm_user.h> */
#import <sys/stat.h>
#import <defaults/defaults.h>
#import <pwd.h>


@implementation Controller

- appDidInit:sender
{
	myListener = [NXApp appListener];
	[myListener setServicesDelegate: self];
	return self;
}

/* Brings up the Info panel.   Not done on startup because it's in a separate
 * interface file. Saves startup time for the user if we do this when they ask
 * for it, and not before.
 * "infoPanel" and "helpPanel" are defined in "Info.nib".
 */
- infoPanel:sender
{
    if( ! infoPanel ){
        [NXApp loadNibSection:"Info.nib" owner:self];
    }
    [infoPanel makeKeyAndOrderFront:sender];
    return self;    
}
/*
 * Brings up the Help panel (as above)
 */
- helpPanel:sender
{
    if( ! helpPanel ){
        [NXApp loadNibSection:"Info.nib" owner:self];
    }
    [helpPanel makeKeyAndOrderFront:sender];
    return self;    

}

extern void import_mailrc();
extern FILE *freopen();

- importMailrcFile:sender;
{
	char *Unixmailrcfn;
	char *NeXTmailaliasfn;
	FILE *old_stdin;
	FILE *old_stdout;
	
	Unixmailrcfn  = malloc(550);
	NeXTmailaliasfn = malloc(550);
	(void)strcpy(Unixmailrcfn, strcpy(NeXTmailaliasfn, 
		                   NXHomeDirectory()));
	(void)strcat(Unixmailrcfn, "/.mailrc");
	(void)strcat(NeXTmailaliasfn, "/.NeXT/.mailalias");
	/* Save stdin and stdout across freopen call */
	old_stdin  = stdin;
	old_stdout = stdout;
        if ((freopen(Unixmailrcfn,"r",stdin))     != NULL)
	if ((freopen(NeXTmailaliasfn,"a",stdout)) != NULL)
            {import_mailrc();
	    /* close open files */
	    fclose(stdin);
	    fclose(stdout);
	    }
	else
          NXRunAlertPanel("Import .mailrc Service Alert","Cannot open mail default files :-(", NULL, NULL, NULL);
	/* Restore stdin and stdout after freopen call */
	/* Is it necessary? And how do you do it? 
        stdin  = old_stdin;
	stdout = old_stdout; */
	return self;
}


extern void export_mailrc();

- exportMailaliasFile:sender /* Ok, just wait a second! :-) */
{
	char *Unixmailrcfn;
	char *NeXTmailaliasfn;
	FILE *old_stdin;
	FILE *old_stdout;
	
	Unixmailrcfn  = malloc(550);
	NeXTmailaliasfn = malloc(550);
	(void)strcpy(Unixmailrcfn, strcpy(NeXTmailaliasfn, 
		                   NXHomeDirectory()));
	(void)strcat(Unixmailrcfn, "/.mailrc");
	(void)strcat(NeXTmailaliasfn, "/.NeXT/.mailalias");
	/* Save stdin and stdout across freopen call */
	old_stdin  = stdin;
	old_stdout = stdout;
        if ((freopen(NeXTmailaliasfn,"r",stdin))     != NULL)
	if ((freopen(Unixmailrcfn,"a",stdout)) != NULL)
            {export_mailrc();
	    /* close open files */
	    fclose(stdin);
	    fclose(stdout);
	    }
	else
          NXRunAlertPanel("Export .mailrc Service Alert","Cannot open mail default files :-(", NULL, NULL, NULL);
	/* Restore stdin and stdout after freopen call */
	/* Is it necessary? And how do you do it? 
        stdin  = old_stdin;
	stdout = old_stdout; */
	return self;
}

- SaveSelection:(id)pbid userData:(const char *)udata error:(char **)errmsg
{
  SavePanel *panel;
  const char *name = NULL, *path = NULL;
  FILE *fpout;
  char *data; 
  int   length;
  int savePanel = 0;
  const char *const *types;
  int   hasAscii, i;
  int rest = 0, fits, divs;
  int bufsize;
  
  bufsize = 1024*64;
  types = [pbid types];		/* get a list of pasteboard types */
  hasAscii = 0;
  for(i=0; !hasAscii && types[i]; i++)
    if(!strcmp(types[i], NXAsciiPboardType)) hasAscii = 1;
  if(hasAscii)
    {
      [pbid readType:NXAsciiPboardType data:&data length:&length];
      if(data && length)
        {
          if (!savePanel) {panel = [SavePanel new];savePanel =1;};
          if ([panel runModalForDirectory:path file:name])
            { 
              if ((fpout = fopen([panel filename],"a")) != NULL)
                {
                  if (length < bufsize) fwrite(data,length,1,fpout);
                  else {fwrite(data,bufsize,(divs = length/bufsize),fpout);
                        if ((rest = (length - (fits = divs*bufsize))) != 0)
                          {fwrite(data+fits,rest,1,fpout);};
                      };
               fclose(fpout); printf ("Saved file from mail\n");
                };
/*              [panel free]; */
            };
        };
    };
  return self;
}

@end
